<template>
  <div class="app-container">
    <el-row class="pagecenter">
      <el-form :inline="true" style="float:right">
        <el-form-item label-width="80px">
          <el-button @click="createAdvertising" v-show="btnVisible.isAdd">
            <span>
              <icon-svg icon-class="xinjian"></icon-svg>&nbsp;&nbsp;新建首页广告</span>
          </el-button>
          <!-- <el-button icon="document" @click="previewAdvertising">预览</el-button> -->
        </el-form-item>
      </el-form>
    </el-row>
    <el-row class="pageborder">
      <el-table :data="list" v-loading.body="listLoading" element-loading-text="拼命加载中" highlight-current-row tooltip-effect="dark" style="width: 100%" :fit="true">
        <el-table-column label='序号' min-width="80" header-align="center" align="center">
          <template scope="scope">
            {{ page.pageSize * (page.currentPage - 1) + scope.$index + 1}}
          </template>
        </el-table-column>
        <el-table-column label="广告位置" min-width="100" header-align="center" align="left">
          <template scope="scope">
            {{scope.row.aplocal}}
          </template>
        </el-table-column>
        <el-table-column label="产品ID-名称" min-width="300" :show-overflow-tooltip="true" header-align="center" align="left">
          <template scope="scope">
            <span>{{scope.row.aptracearchivesid}}-{{scope.row.proName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="排序编号" min-width="100" header-align="center" align="center">
          <template scope="scope">
            {{scope.row.aporderno}}
          </template>
        </el-table-column>
        <el-table-column label="操作账号" min-width="130" :show-overflow-tooltip="true" header-align="center" align="left">
          <template scope="scope">
            {{scope.row.crtAccount}}
          </template>
        </el-table-column>
        <el-table-column label="设置时间" min-width="180" header-align="center" align="left">
          <template scope="scope">
            {{scope.row.apcreatedate}}
          </template>
        </el-table-column>
        <el-table-column label="当前状态" min-width="120" header-align="center" align="center">
          <template scope="scope">
            {{scope.row.stateName}}
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="270" header-align="center" align="center">
          <template scope="scope">
            <el-button icon="edit" type="small" @click="advertiseEdit(scope.$index,scope.row)" v-show="btnVisible.isEdit">编辑</el-button>
            <el-button type="small" @click="advertiseStatusSet(scope.$index,scope.row)" v-show="btnVisible.isEnable">
              <span>
                <icon-svg v-show="scope.row.apstatus === '0'" icon-class="jinyong"></icon-svg>
                <icon-svg v-show="scope.row.apstatus !== '0'" icon-class="qiyong">
                </icon-svg>&nbsp;&nbsp;{{scope.row.apstatus === "0" ? "禁用" : "启用"}}
              </span>
            </el-button>
            <el-button icon="delete" type="small" @click="deleteAdvertise(scope.$index,scope.row)" v-show="btnVisible.isDelete">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-row class="pagination" v-if="page.total > 0 ? true : false">
      <el-pagination @current-change="handleCurrentChange" :page-size="page.pageSize" :current-page.sync="page.currentPage" layout="total, prev, pager, next, jumper" :total="page.total">
      </el-pagination>
    </el-row>
  </div>
</template>

<style scoped>
.pagination {
  text-align: center;
}

.el-col {
  border-radius: 4px;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}

.bg-purple-dark {
  background: #99a9bf;
}

.colSpace {
  margin-left: 5px;
}

td {
  overflow: hidden;
  text-overflow: ellipsis;
}

.viewBtn {
  float: right;
}

.selectStyle {
  width: 145px;
}

.el-form-item {
  margin-bottom: 15px;
}
</style>

<script>
import { getList, saveAdvertising, deleteAdvertising } from 'api/advertise';
import { parseTime } from 'utils/index';
import { btnIsVisible } from 'utils/customer';

export default {
  data() {
    return {
      list: null,
      listQuery: {},
      page: {
        pageSize: null,
        total: null,
        totalPage: null,
        currentPage: null
      },
      listLoading: true,
      btnVisible: {
        isAdd: false,
        isEdit: false,
        isDelete: false,
        isEnable: false,
      },
    }
  },
  created() {
    // this.getProductType();
    this.fetchData();
    this.getBtnVisbleStatus();
  },
  methods: {
    getBtnVisbleStatus() {
      this.btnVisible.isEnable = btnIsVisible('HOMEPAGE_ADVERT_START_STOP') || false;
      this.btnVisible.isAdd = btnIsVisible('HOMEPAGE_ADVERT_INSERT') || false;
      this.btnVisible.isEdit = btnIsVisible('HOMEPAGE_ADVERT_UPDATE') || false;
      this.btnVisible.isDelete = btnIsVisible('HOMEPAGE_ADVERT_DELETE') || false;
    },
    // 查询广告列表
    fetchData() {
      this.listLoading = true;
      this.page.pageSize = this.page.pageSize || 10;
      this.page.currentPage = this.page.currentPage || 1;
      this.listQuery.currentPage = this.page.currentPage;
      this.listQuery.pageSize = this.page.pageSize;
      this.listQuery.aptype = 'ADV_HOMEPAGE';
      getList(this.listQuery).then(response => {
        let res = response.data;
        if (res.flag) {
          this.list = res.data;
          this.page = res.page;
        }
        this.listLoading = false;
      })
    },
    // 新建广告
    createAdvertising() {
      this.$router.push({ path: 'advertisementadd' });
    },
    // 广告状态编辑
    advertiseStatusSet(index, row) {
      let requsestParam = {
        apid: row.apid,
        apstatus: row.apstatus === "0" ? "1" : "0",
      }
      saveAdvertising(requsestParam).then(response => {
        let res = response.data;
        if (res.flag === 1) {
          row.stateName = requsestParam.apstatus === "0" ? "正常" : "禁用";
          row.apstatus = requsestParam.apstatus;
          this.$message({
            showClose: true,
            message: "产品广告" + (row.apstatus === "0" ? "启用" : "禁用") + "成功",
            type: 'success'
          });
        } else {
          this.$message({
            showClose: true,
            message: "产品广告" + (row.apstatus === "0" ? "启用" : "禁用") + "失败",
            type: 'error'
          });
        }
      })
    },
    // 广告删除
    deleteAdvertise(index, row) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let requsestParam = {
          apid: row.apid
        }
        deleteAdvertising(requsestParam).then(response => {
          let res = response.data;
          if (res.flag === 1) {
            this.fetchData();
            this.$message({
              showClose: true,
              message: "产品广告删除成功",
              type: 'success'
            });
          } else {
            this.$message({
              showClose: true,
              message: "产品广告删除失败",
              type: 'error'
            });
          }
        })
      }).catch(() => {
      });

      // let requsestParam = {
      //   apid: row.apid
      // }
      // deleteAdvertising(requsestParam).then(response => {
      //   let res = response.data;
      //   if (res.flag === 1) {
      //     this.fetchData();
      //     this.$message({
      //       showClose: true,
      //       message: "产品广告删除成功",
      //       type: 'success'
      //     });
      //   } else {
      //     this.$message({
      //       showClose: true,
      //       message: "产品广告删除失败",
      //       type: 'error'
      //     });
      //   }
      // })
    },
    // 广告编辑
    advertiseEdit(index, row) {
      this.$router.push({ path: 'advertisementedit', query: { advertiseInfo: row } });
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    handleDivisionChange(val) {
      // console.log(val);
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.page.currentPage = val;
      this.fetchData();
    }
  }
};
</script>